package com.tom.demo.common.config;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;



/**
 * Sa-Token配置全局拦截器
 */
@Slf4j
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
    /**
     * 不需要拦截的资源和请求
     */
    private static final String[] releasePaths = {
            "/user/login",
            "/assets/**",
            "/resources/**",
            "/static/**",
            "/swagger-ui.html",
            "/favicon.ico",
            "//error",
            "/swagger/**",
            "/webjars/**",
            "/swagger-resources/**",
            "/v2/**",
            "/configuration/security",
            "/configuration/ui",
            "/doc.html",
            "/public/**",
            "/file/**",
            "/api/thirdParty/**",
    };

    /**
     * 注册拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                .addPathPatterns("/**")
                .excludePathPatterns(releasePaths);
    }

}
